package com.wcity.manage.service.community.survey.impl;

import com.wcity.manage.common.enums.SaveImageType;
import com.wcity.manage.common.vo.community.survey.SurveyVo;
import com.wcity.manage.dao.mapper.community.survey.SurveyMapper;
import com.wcity.manage.dao.model.community.image.ImageUrl;
import com.wcity.manage.dao.model.community.survey.Survey;
import com.wcity.manage.service.community.image.ImageService;
import com.wcity.manage.service.community.survey.SurveyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 社区概况
 * Created by hgg on 2016/12/15.
 */
@Service
public class SurveyServiceImpl implements SurveyService{

    @Autowired
    private SurveyMapper surveyMapper;

    @Autowired
    private ImageService imageService;

    /**
     * 社区概况分页
     * @param requestMap
     * @return
     */
    @Override
    public List<SurveyVo> surveys( Map<String, Object> requestMap ) {

        return surveyMapper.surveyPageList( requestMap );
    }

    /**
     * 社区概况总数量
     * @param requestMap
     * @return
     */
    @Override
    public Integer surveyTotal( Map<String, Object> requestMap ) {

        return surveyMapper.surveyTotal(requestMap);
    }

    /**
     * 删除社区概况
     * @param requestMap
     * @return
     */
    @Transactional( propagation = Propagation.REQUIRED, isolation = Isolation.SERIALIZABLE,rollbackForClassName = "Exception" )
    @Override
    public Integer deleteSurvey( Map<String, Object> requestMap ) {

        //【1】删除小区概况
        Survey survey = surveyMapper.surveyDetail( requestMap );
        requestMap.put( "id",survey.getParentId() );
        Integer deleteNum = surveyMapper.deleteSurvey( requestMap );
        if( deleteNum == 0 ){
            throw new RuntimeException( "删除社区概况失败" );
        }

        //【2】删除社区概况
        requestMap.put( "parentId",survey.getParentId() );
        requestMap.put( "id",null );
        deleteNum = surveyMapper.deleteSurvey( requestMap );
        if( deleteNum == 0 ){
            throw new RuntimeException( "删除小区概况失败" );
        }
        return 1;
    }

    /**
     * 新增社区概况
     * @param survey
     * @return
     */
    @Transactional( propagation = Propagation.REQUIRED, isolation = Isolation.SERIALIZABLE,rollbackForClassName = "Exception" )
    @Override
    public Integer saveAddSurvey( Survey survey,String[] blockIds,String imageUrl ) {

        Integer parentId = 0;
        //【1】生成父社区概况
        survey.setParentId( 0 );
        survey.setCommunityId( Integer.parseInt( blockIds[0] ) );//初始化一个小区ID
        Integer saveParentNum = surveyMapper.saveAddSurvey( survey );
        if( saveParentNum == 0 ){
            throw new RuntimeException("保存社区概况失败");
        }
        parentId = survey.getId();

        //【2】执行保存社区封面
        ImageUrl image = new ImageUrl( null,imageUrl,null,parentId,SaveImageType.COMMUNITY_SURVEY );
        Integer saveImageNum = imageService.saveAddImage( image );
        if( saveImageNum == 0 ){
            throw new RuntimeException( "保存小区封面图片失败" );
        }

        //【3】执行新增小区概况
        saveAddSurvey( survey,blockIds,parentId,imageUrl );
        return 1;
    }

    /**
     * 更新社区概况
     * @param survey
     * @return
     */
    @Transactional( propagation = Propagation.REQUIRED, isolation = Isolation.SERIALIZABLE,rollbackForClassName = "Exception" )
    @Override
    public Integer saveEditSurvey( Survey survey,String[] blockIds,String imageUrl ) {

        Integer parentId = survey.getParentId();
        //【1】删除以前社区封面
        Map<String,Object> deleteMap = new HashMap<>();
        deleteMap.put("referId", survey.getId());
        deleteMap.put( "saveType", SaveImageType.COMMUNITY_SURVEY );
        imageService.deleteImage( deleteMap );

        //【2】删除以前的小区概况
        Map<String,Object> deleteSurveyMap = new HashMap<>();
        deleteSurveyMap.put( "parentId",survey.getParentId() );
        deleteSurveyMap.put( "userId",survey.getUserId() );
        Integer deleteNum = surveyMapper.deleteSurvey( deleteSurveyMap );
        if( deleteNum == 0 ){
            throw new RuntimeException( "删除社区概况失败" );
        }

        //【4】执行更新社区概况(更新父级)
        Survey parent = survey;
        parent.setCommunityId( Integer.parseInt( blockIds[0] ) );
        parent.setId( survey.getParentId() );
        Integer updateNum = surveyMapper.saveEditSurvey( parent );
        if( updateNum == 0 ){
            throw new RuntimeException( "更新社区概况失败 ");
        }

        //【5】执行新增小区概况
        saveAddSurvey( survey,blockIds,parentId,imageUrl );
        return 1;
    }

    /**
     * 社区概况详情
     * @param requestMap
     * @return
     */
    @Override
    public Survey surveyDetail( Map<String, Object> requestMap ) {

        return surveyMapper.surveyDetail(requestMap);
    }

    /**
     * 社区概况列表（小区）
     * @param requestMap
     * @return
     */
    @Override
    public List<Survey> surveyList( Map<String, Object> requestMap ) {

        return surveyMapper.surveyList(requestMap);
    }

    /**
     * 执行新增小区概况
     * @param survey
     * @param blockIds
     */
    private void saveAddSurvey( Survey survey,String[] blockIds,Integer parentId,String imageUrl ){

        for ( String id : blockIds ){
            survey.setCommunityId( Integer.parseInt(id ) );
            //【6】执行保存
//            survey.setCommunityContent(null);
            survey.setParentId(parentId);
//            survey.setRemark(null);
            Integer saveNum = surveyMapper.saveAddSurvey( survey );
            if( saveNum == 0 ){
                throw new RuntimeException("保存社区概况失败");
            }
            //【3】执行保存社区封面
            ImageUrl image = new ImageUrl( null,imageUrl,null,survey.getId(),SaveImageType.COMMUNITY_SURVEY );
            Integer saveImageNum = imageService.saveAddImage( image );
            if( saveImageNum == 0 ){
                throw new RuntimeException( "保存社区封面图片失败" );
            }
        }
    }

    /**
     * 查询小区ID拼接（1,2,3）
     * @param queryMap
     * @return
     */
    public String blockIds( Map<String,Object> queryMap ){

        return surveyMapper.blockIds( queryMap );
    }
}
